package com.jcl.recruit.resume.logic;

import com.jcl.core.utils.DateUtils;
import com.jcl.recruit.resume.mapper.ResumeMapper;
import com.jcl.recruit.resume.model.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ResumeLogic
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/25 14:23
 * @Version 1.0
 **/
@Component
public class ResumeLogic implements ResumeMapper {

    @Autowired
    private ResumeMapper      resumeMapper;

    @Override
    public List<Map<String,Object>> selectQueryResumes(String keyword,
                                                       String education,
                                                       String sex,
                                                       Integer workyear,
                                                       String city,
                                                       Date dateBegin,
                                                       Date dateEnd,
                                                       String sourceSub,
                                                       Integer status,
                                                       Integer userId,
                                                       Integer ismanual) {
        List<Map<String,Object>> list = resumeMapper.selectQueryResumes(keyword,education,sex,workyear,
                city,dateBegin,dateEnd,sourceSub,status,userId,ismanual);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = sdf.format(new Date());
        Calendar aCalendar = Calendar.getInstance();
        for(Map<String,Object> entity:list){
            if(entity.get("apply_date") != null) {
                aCalendar.setTime(new Date());
                int dayOne = aCalendar.get(Calendar.DAY_OF_YEAR);
                aCalendar.setTime((Date) entity.get("apply_date"));
                int dayTwo = aCalendar.get(Calendar.DAY_OF_YEAR);
                entity.put("apply_rest_date", dayOne - dayTwo + 1);
                entity.put("apply_date", sdf.format(entity.get("apply_date")));
            }
            Integer age = null;
            if(entity.get("birthday") != null) {
                age = DateUtils.formatAge(entity.get("birthday").toString());
            }
            entity.put("age",age);
        }
        return list;
    }

    @Override
    public List<Map<String,Object>> selectCountResume(String keyword, String education, String sex, Integer workyear, String city,
                                                      Date dateBegin, Date dateEnd, String sourceSub, Integer status,Integer userId,Integer ismanual) { ;
        return resumeMapper.selectCountResume(keyword,education,sex,workyear,city,dateBegin,dateEnd,sourceSub,status,userId,ismanual);
    }

    @Override
    public Map<String, Object> selectResumeByPrimsryKeyIdMap(Integer id) {
        return resumeMapper.selectResumeByPrimsryKeyIdMap(id);
    }

    @Override
    public List<Map<String, Object>> selectAttentionResumesByCondition(List<Integer> resumeIds) {
        return resumeMapper.selectAttentionResumesByCondition(resumeIds);
    }

    @Override
    public int insert(Resume resume) {
        int result = resumeMapper.insert(resume);
        return result;
    }

    @Override
    public Resume countNameAndMobile(String realname, String mobile) {
        return resumeMapper.countNameAndMobile(realname,mobile);
    }

    @Override
    public int updateByPrimaryKeySelective(Resume record) {
        return resumeMapper.updateByPrimaryKeySelective(record);
    }

    public Map<String,Object> selectPrimaryKey(Integer id){
        return resumeMapper.selectResumeByPrimsryKeyIdMap(id);
    }

    @Override
    public int batchUpdate(List<Resume> list) {
        return resumeMapper.batchUpdate(list);
    }

    @Override
    public Resume selectPrimarykeyByEntity(Integer id) {
        return resumeMapper.selectPrimarykeyByEntity(id);
    }

    @Override
    public Resume selectPrimarykeyByApplyOtherJid(String corpCode, String applyOtherId) {
        return resumeMapper.selectPrimarykeyByApplyOtherJid(corpCode, applyOtherId);
    }
}
